/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.components;

import plash.display.Graphics;
import plash.display.Sprite;
import plash.events.Event;
import plash.events.IEventListener;
import plash.events.MouseEvent;
import plash.geom.Point;
import plash.text.TextField;

/**
 *
 * @author #0K Srinivasan <hashok.com>
 */
public class ComboBox extends Sprite implements IEventListener
{
    private ComboBoxButton _button = new ComboBoxButton();
    private ListBox _list;

    public ComboBox()
    {
        this(new String[0]);
    }

    public ComboBox(String[] items)
    {
        super();
        _list = new ListBox(items);
        _initialize();
    }

    private void _initialize()
    {
        _button.setSize(100, 20);
        _list.setListWidth(100);
        
        _button.setValue(_list.getValue());
        _list.setVisible(false);
        
        _list.setY(_button.getY() + _button.getItemHeight());
        this.addChild(_list);
        this.addChild(_button);
        
        _list.addEventListener("onItemSelect", this);
        _button.addEventListener(MouseEvent.MOUSE_UP, this);
        addEventListener(MouseEvent.MOUSE_DOWN, this);
    }

    public void setSize(int width, int height)
    {
        _list.setListWidth(width);
    }
    
    public int getIndex()
    {
        return _list.getIndex();
    }
    
    public void setIndex(int index)
    {
        _list.setIndex(index);
        _button.setValue(_list.getValue());
    }
    
    public String getValue()
    {
        return _list.getValue();
    }
    
    public int getListWidth()
    {
        return _list.getListWidth();
    }

    public void setListWidth(int listWidth)
    {
        _list.setListWidth(listWidth);
        _button.setItemWidth(listWidth);
    }
    
    public int getListItemHoverColor()
    {
        return _list.getItemHoverColor();
    }

    public void setListItemHoverColor(int itemHoverColor)
    {
        _list.setItemHoverColor(itemHoverColor);
    }

    public int getListItemNormalColor()
    {
        return _list.getItemNormalColor();
    }

    public void setListItemNormalColor(int itemNormalColor)
    {
        _list.setItemNormalColor(itemNormalColor);
    }

    public int getListItemClickColor()
    {
        return _list.getItemClickColor();
    }

    public void setListItemClickColor(int itemClickColor)
    {
        _list.setItemClickColor(itemClickColor);
    }
    
    public void setSelectorNormalColor(int normalColor)
    {
        _button.setItemNormalColor(normalColor);
    }
    
    public int getSelectorNormalColor()
    {
        return _button.getItemNormalColor();
    }
    
    public void setSelectorHoverColor(int hoverColor)
    {
        _button.setItemHoverColor(hoverColor);
    }
    
    public int getSelectorHoverColor()
    {
        return _button.getItemHoverColor();
    }
    
    public void setSelectorClickColor(int clickColor)
    {
        _button.setItemClickColor(clickColor);
    }
    
    public int getSelectorClickColor()
    {
        return _button.getItemClickColor();
    }

    @Override
    public void event(Event e)
    {
        if(e.target == _list)
        {
            if(e.name.equals("onItemSelect"))
            {
                _list.setVisible(false);
                _button.setValue(_list.getValue());
                dispatchEvent(new Event("onItemSelect"));
                return;
            }
        }
        else if(e.target == _button)
        {
            if(e.name.equals(MouseEvent.MOUSE_UP))
            {
                System.out.println("TOGGLE VISIBILITY "+_button.getBoundingBox());
                _list.setVisible(!_list.isVisible());
            }
        }
        if(e.target == this)
        {
            if(e.name.equals(MouseEvent.MOUSE_DOWN))
            {
                if(!this.getBoundingBox().containsPoint(this.getParent().getMouseX(), this.getParent().getMouseY()))
                {
                    _list.setVisible(false);
                }
            }
        }
    }

    /**
     * Start of the ListItem Class. This class is a private class and is not
     * accessible outside the ListClass
     */
    class ComboBoxButton extends Sprite implements IEventListener
    {
        private int _itemWidth = 50;
        private int _itemHeight = 50;
        private int _itemHoverColor = 0xFFFFFF;
        private int _itemNormalColor = 0xFFFFFF;
        private int _itemClickColor = 0xFFFFFF;
        
        private Point _arrowPointOne = new Point();
        private Point _arrowPointTwo = new Point();
        private Point _arrowPointThree = new Point();
        
        private String _value = "";
        private TextField _field = new TextField();
        private Sprite _listItemBackground = new Sprite();

        public ComboBoxButton()
        {
            super();
            _initialize();
        }

        private void _initialize()
        {
            _listItemBackground.addEventListener(MouseEvent.MOUSE_OVER, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_OUT, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_DOWN, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_UP, this);

            this.addChild(_listItemBackground);
            this.addChild(_field);

            _setRollOutState();
            _invalidate();
        }

        private void _invalidate()
        {
            _updateArrowPoints();
            _field.setText(_value);
            _field.setWidth(_itemWidth);
            _field.setHeight(_itemHeight);
            _setRollOutState();
        }
        
        private void _updateArrowPoints()
        {
            _arrowPointOne.setX((int)(this._itemWidth * 0.95));
            _arrowPointOne.setY((int)(this._itemHeight * 0.25));
            
            _arrowPointTwo.setX((int)(this._itemWidth * 0.85));
            _arrowPointTwo.setY((int)(this._itemHeight * 0.25));
            
            _arrowPointThree.setX((int)(this._itemWidth * 0.90));
            _arrowPointThree.setY((int)(this._itemHeight * 0.75));
        }

        private void _setRollOverState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemHoverColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
            
            g.setLineStyle(1, 0xFFFFFF, 1.0f);
            g.beginFill(0x000000, 1.0f);
            g.drawTriangle(_arrowPointOne, _arrowPointTwo, _arrowPointThree);
            g.endFill();
        }

        private void _setRollOutState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemNormalColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
            g.beginFill(0x000000, 1.0f);
            g.drawTriangle(_arrowPointOne, _arrowPointTwo, _arrowPointThree);
            g.endFill();
        }

        private void _setMouseDownState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemClickColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
            g.beginFill(0x000000, 1.0f);
            g.drawTriangle(_arrowPointOne, _arrowPointTwo, _arrowPointThree);
            g.endFill();
        }

        @Override
        public void event(Event e)
        {
            if (e.target == _listItemBackground)
            {
                if (e.name.equals(MouseEvent.MOUSE_OVER))
                {
                    _setRollOverState();
                }
                else if (e.name.equals(MouseEvent.MOUSE_OUT))
                {
                    _setRollOutState();
                }
                else if (e.name.equals(MouseEvent.MOUSE_DOWN))
                {
                    _setMouseDownState();
                }
                else if (e.name.equals(MouseEvent.MOUSE_UP))
                {
                    _setRollOutState();
                }
            }
        }

        public void setSize(int width, int height)
        {
            this._itemWidth = width;
            this._itemHeight = height;
            _invalidate();
        }

        public int getItemWidth()
        {
            return _itemWidth;
        }

        public void setItemWidth(int itemWidth)
        {
            this._itemWidth = itemWidth;
            _invalidate();
        }

        public int getItemHeight()
        {
            return _itemHeight;
        }

        public void setItemHeight(int itemHeight)
        {
            this._itemHeight = itemHeight;
            _invalidate();
        }

        public String getValue()
        {
            return _value;
        }

        public void setValue(String value)
        {
            this._value = value;
            _invalidate();
        }

        public int getItemHoverColor()
        {
            return _itemHoverColor;
        }

        public void setItemHoverColor(int itemHoverColor)
        {
            this._itemHoverColor = itemHoverColor;
        }

        public int getItemNormalColor()
        {
            return _itemNormalColor;
        }

        public void setItemNormalColor(int itemNormalColor)
        {
            this._itemNormalColor = itemNormalColor;
        }

        public int getItemClickColor()
        {
            return _itemClickColor;
        }

        public void setItemClickColor(int itemClickColor)
        {
            this._itemClickColor = itemClickColor;
        }
    }
}
